/*
 * @Author: your name
 * @Date: 2021-04-16 20:25:07
 * @LastEditTime: 2021-04-17 10:16:03
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /flutter_demo/lib/pages/RefreshData.dart
 */
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'dart:convert';

class RefreshDataPage extends StatefulWidget {
  @override
  _RefreshDataPageState createState() => _RefreshDataPageState();
}

class _RefreshDataPageState extends State<RefreshDataPage> {
  List _list = [];
  bool _flag = true;
  int _page = 1;
  ScrollController _scrollController = new ScrollController();

  Widget _getMoreWidget() {
    if (_flag) {
      return Center(
        child: Padding(
          padding: EdgeInsets.all(10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text('加载中...', style: TextStyle(fontSize: 16)),
              CircularProgressIndicator(
                strokeWidth: 1,
              )
            ],
          ),
        ),
      );
    } else {
      return Center(
        child: Text('--我是有底线的--'),
      );
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this._getData();

    _scrollController.addListener(() {
      var _top = _scrollController.position.pixels; // 滚动条滚动高度
      var _scrollY = _scrollController.position.maxScrollExtent; // 页面高度
      if (_top > _scrollY - 40) {
        this._getData();
      }
    });
  }

  _getData() async {
    if (_flag) {
      var url =
          'http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=${_page}';
      var response = await Dio().get(url);
      var res = json.decode(response.data)['result'];
      setState(() {
        _list.addAll(res);
        this._page++;
      });

      if (res.length < 20) {
        setState(() {
          _flag = false;
        });
      }
    }
  }

  Future<void> onRefresh() async {
    await Future.delayed(Duration(milliseconds: 2000), () {});
    _getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('上拉下拉刷新'),
        ),
        body: this._list.length > 0
            ? RefreshIndicator(
                onRefresh: onRefresh,
                child: ListView.builder(
                  controller: _scrollController,
                  itemCount: this._list.length,
                  itemBuilder: (context, index) {
                    if (index == this._list.length - 1) {
                      return Column(
                        children: <Widget>[
                          ListTile(
                            title: Text(
                              '${this._list[index]["title"]}',
                              maxLines: 1,
                            ),
                          ),
                          Divider(),
                          _getMoreWidget()
                        ],
                      );
                    } else {
                      return Column(
                        children: <Widget>[
                          ListTile(
                            title: Text(
                              '${this._list[index]["title"]}',
                              maxLines: 1,
                            ),
                            onTap: () {
                              Navigator.pushNamed(context, '/newContents',
                                  arguments: {'aid': this._list[index]['aid']});
                            },
                          ),
                          Divider()
                        ],
                      );
                    }
                  },
                ),
              )
            : _getMoreWidget());
  }
}
